package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.Echart;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/echart")
@Slf4j
public class EchartController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    @PostMapping("/data")
    public Echart getData(){

        Category category=new Category();
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Category::getType,1);
        List<Category> list = categoryService.list(lqw);
        Echart echart=new Echart();

        for (Category one : list) {
            echart.getName().add(one.getName());
            LambdaQueryWrapper<Dish> lqw2=new LambdaQueryWrapper<>();
            lqw2.eq(Dish::getCategoryId,one.getId());
            Integer count = dishService.count(lqw2);
            echart.getNumber().add(count);
        }
        return echart;
    }

    @PostMapping("/data2")
    public Echart getData2(){

        Category category=new Category();
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Category::getType,2);
        List<Category> list = categoryService.list(lqw);
        Echart echart=new Echart();

        for (Category one : list) {
            echart.getName().add(one.getName());
            LambdaQueryWrapper<Setmeal> lqw2=new LambdaQueryWrapper<>();
            lqw2.eq(Setmeal::getCategoryId,one.getId());
            Integer count = setmealService.count(lqw2);
            echart.getNumber().add(count);
        }
        return echart;
    }
}
